package com.es.utils;

import com.zaxxer.hikari.HikariDataSource;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 描述:
 * es-servlet-
 *
 * @author Derek
 * 创建时间：2022-04-20 11:35
 */
public class DBUtils {
    private static String driverClassName = "com.mysql.cj.jdbc.Driver";
    private static String url = "jdbc:mysql://127.0.0.1:3306/jdbc2201";
    private static String user = "root";
    private static String password = "root";
    private static int maxActive = 30;
    private static int minIdle = 5;
    private static long maxWait = 30000;

    private static HikariDataSource dataSource;
    static{
        dataSource = new HikariDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setJdbcUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        dataSource.setMaximumPoolSize(maxActive);
        dataSource.setMinimumIdle(minIdle);
        dataSource.setConnectionTimeout(maxWait);
    }
    public static Connection getConnection() throws SQLException {
        if(dataSource == null){
            throw new RuntimeException("数据源有问题");
        }else{
            return dataSource.getConnection();
        }
    }

    public static void main(String[] args) {
        try {
            System.out.println(DBUtils.getConnection());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
